package cn.knight.basic.controller;

import cn.knight.basic.service.ILoginService;
import cn.knight.basic.utils.AjaxResult;
import cn.knight.basic.vo.LoginVo;
import cn.knight.user.dto.WechatDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
    * @Title: account
    * @Description: 根据用户名进行登录
    * @Author: winnie
    * @Version: 1.0
    * @Date:  7/7/2022 下午11:42
    * @Parameters: [loginVo]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginVo loginVo) {
        try {
            Map<String,Object> map = loginService.account(loginVo);
            return AjaxResult.me().setResultObj(map);
        } catch (RuntimeException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: wechat
    * @Description: 微信扫码登录
    * @Author: winnie
    * @Version: 1.0
    * @Date:  2022/7/10 16:36
    * @Parameters: [wechatDto]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDto wechatDto) {
        try {
            // 直接返回调用，在service实现层封装前端需要的数据
            return loginService.wechat(wechatDto);
        } catch (RuntimeException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatDto wechatDto) {
        try {
            Map<String,Object> map = loginService.binder(wechatDto);
            return AjaxResult.me().setResultObj(map);
        } catch (RuntimeException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
